<?php
/**
 * 图形验证码验证类
 * User: dxk
 * Date: 2020/07/15
 */
namespace App\Library\Service;

class Captcha
{
    /**
     * 设置验证码内容
     * @param string $captcha
     * @return bool
     */
    public static function storeCaptcha($captcha): bool
    {
        $captcha = trim($captcha);
        if (empty($captcha)) {
            return false;
        }
        session(['phrase' => $captcha]);
        \Session::save();
        return true;
    }

    /**
     * 验证图形验证码是否正确
     * @param string $user_captcha
     * @return boolean
     */
    public static function verifyCaptcha(string $user_captcha): bool
    {
        $user_captcha = trim(strtolower($user_captcha));
        if (empty($user_captcha)) {
            return false;
        }
        $captcha = strtolower(session('phrase'));
        if ($captcha != $user_captcha) {
            return false;
        } else {
            \Session::forget('phrase');
            return true;
        }
    }
}
